﻿package com.admin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.common.BaseController;
import com.entity.Menu;
import com.service.MenuService;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {
	
	@Autowired
	private MenuService menuService;
	
	/**
	 * 后台管理首页
	 * @return
	 */
	@RequestMapping("/index")
	public String index(){
		return "admin.index";
	}
	
	@RequestMapping("/menu")
	public String menu(HttpServletRequest request, HttpServletResponse response){
		String id = request.getParameter("id");
		if(StringUtils.isEmpty(id)){
			id = "0";
		}
		List<Menu> list = this.menuService.findByParent(Integer.parseInt(id));
		JSONArray array = new JSONArray();
		for(Menu menu:list){
			JSONObject obj = new JSONObject();
			obj.put("id", menu.getId());
			obj.put("text", menu.getText());
			obj.put("iconCls", menu.getIconCls());
			obj.put("url", menu.getUrl());
			obj.put("leaf", menu.getLeaf());
			obj.put("state", menu.getState());
			array.add(obj);
		}
		this.outJson(array.toJSONString(), response);
		return null;
	}
}
